// api/getAddres.js

import store from '../store/index.js';
var jweixin = require('jweixin-module');
import jwx from '@/utils/jwx.js'
//获取位置
export function getLocation() {
	
  return new Promise((resolve, reject) => {
	  // 小程序和其他网页
  //   uni.getLocation({
  //     type: 'gcj02',
  //     success: (res) => {
  //       let location = res.latitude + ',' + res.longitude;
  //       let latitude = res.latitude;
  //       let longitude = res.longitude;
		// console.log(location,'+++++++++++++')
  //       resolve(location);
  //     },
  //     fail: (err) => {
  //       reject(err);
  //     }
  //   });
	 // console.log(location,'+++++address+++')
	 //jssdk 微信H5
				jwx.initJssdk();
	 		  jweixin.ready(function() {
				  jweixin.getLocation({
				  	 		  	type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'  
				  	 		  	success: function(res) {
				  	 		  		 let location = res.latitude + ',' + res.longitude;
				  	 				 resolve(location);
				  	 		  	},
				  	 		  	fail: function(res) {
				  	 		  		reject(res);
				  	 		  	},
				  	 		  });
			  })
	 
  });
}
//2.获取地址
export function getAddress(location) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `api?location=${location}&key=WV7BZ-JEJWL-DIYPA-MDJZL-TAW43-2LFNO&get_poi=1`,
      success: (res) => {
        // console.log(res, '+++++++++++++');
        let address = res.data.result;
        // console.log(address);
        resolve(address);
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
}
//翻译地址
export function Translation(text) {
let tgt_text = 'ئىرىشىش مەغلۇپ بولدى';
	return new Promise((resolve, reject) => {
	        uni.request({
	            url: '/niutrans',
	            method: 'POST',
	            data: {
	                from: 'zh',
	                src_text: text,
	                to: 'uy',
	                apikey: 'a743e753d4825fd89b7c1e96b8da9f3a',
	            }
	        }).then(res => {
	            const tgt_text = res.data.tgt_text;
	            // console.log(tgt_text, '++++++++++++++');
	            resolve(tgt_text);
	        }).catch(err => {
	            console.error(err);
	            reject(err);
	        });
	    });
}

